package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_XwScecollect;
import com.xwdeshj.entity.XwFarmCollectinfo;
import com.xwdeshj.entity.XwSceincCollectinfo;
import com.xwdeshj.repository.XwSceincCollectinfoReository;
import com.xwdeshj.service.XwSceincCollectinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by BKWJA on 2019/1/7.
 * 景区——景区收藏信息 实现类型
 */
@Slf4j
@Service("xwSceincCollectinfoService")
public class XwSceincCollectinfoServiceImpl implements XwSceincCollectinfoService {

    @Autowired
    private XwSceincCollectinfoReository xwSceincCollectinfoReository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private Sid sid;

    Dal_XwScecollect dal_xwScecollect = new Dal_XwScecollect();

    /**
     *  获取景区店铺  收藏信息
     */
    @Override
    public JSONObject getSceinccollectlist(Integer pageindex, Integer pagesize, String starttime, String endtime,
                                           String sortfild, String sortway, String searchval, String searchkey,
                                           String sceinc_encryptId,String user_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwScecollect.getSceinccollectlist(pageindex,pagesize,starttime,endtime,sortfild,sortway,
                searchval,searchkey,sceinc_encryptId,user_encryptId,jdbcTemplate);

        System.out.println("getSceinccollectlist:"+result.toString());
        return result;
    }


    /**
     * 景区——取消收藏
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject delCollectSceinc(Integer collect_id, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid = helper.ReadCookieMap(request,"uid").toString();//读取当前操作人用户id
        String u_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();//读取当前操作人用户加密id
        if((!"".equals(uid) && uid != null) && (!"".equals(u_encryptid) && u_encryptid != null)){

            XwSceincCollectinfo xwSceincCollectinfo = new XwSceincCollectinfo();
            xwSceincCollectinfo.setScenicCollectId(collect_id);
            boolean temp = xwSceincCollectinfoReository.exists(Example.of(xwSceincCollectinfo));
            if(temp){
                try {
                    xwSceincCollectinfoReository.deleteByScenicCollectId(collect_id);
                    result.put("status",0);
                    result.put("msg","ok");
                }catch (Exception e){
                    e.printStackTrace();
                    result.put("status",40002);
                    result.put("msg",e);
                }

            }else {
                result.put("status",40002);
                result.put("msg","该记录不存在！");
            }

        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空，请先登录后再操作！");
        }
        System.out.println("delCollectSceinc:"+result.toString());
        return result;
    }


    public Dal_XwScecollect getDal_xwScecollect() {
        return dal_xwScecollect;
    }

    public void setDal_xwScecollect(Dal_XwScecollect dal_xwScecollect) {
        this.dal_xwScecollect = dal_xwScecollect;
    }
}
